package com.crawler58.service;

import java.util.List;
import java.util.concurrent.BlockingQueue;

import com.crawler58.entity.Base;

public class FetchWorker extends Thread {

	private BlockingQueue<Base> inQueue;
	private BlockingQueue<Base> outQueue; 
	private String js;
	
	public FetchWorker(BlockingQueue<Base> inQueue, BlockingQueue<Base> outQueue, String js) {
		this.inQueue = inQueue;
		this.outQueue = outQueue;
		this.js = js;
	}
	
	public void run() {
		while(true) {
			try {
				Base base = inQueue.take();
				List<Base> result = fetch(base, js);
				if(outQueue != null) 
					for(Base re : result) {
						System.out.println(re);
						outQueue.put(re);
					}
						
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	private List<Base> fetch(Base base, String js) {
		List list = base.fetchMe(js);
		return (List<Base>) list;
	}
	
}
